import {useEffect, useState } from "react"
import apiClient from './apiClient'

export default(id) =>{
  const [data,setData] = useState(null);
  const [loading,setLoading] = useState(false);
  const [error,setError] = useState(null);

  useEffect(()=>{
    if(!id) return;
    setLoading(true);
    setData(null);
    setError(null);
    apiClient
      .get(`/users/${id}`)
      .then((res) => { setLoading(false); setData(res.data)
      })
      .catch((err)=>{
        setLoading(false);
        setError(err);
      });
  },[id]);

  return { loading,data,error}
}